//
//  AppDelegate.m
//  BaseAPP
//
//  Created by Evisu on 16/11/20.
//  Copyright © 2016年 evisum. All rights reserved.
//

#import "AppDelegate.h"
#import "MainTabBarController.h"
#import "DMLoginViewController.h"
#import "EMSDK.h"

@interface AppDelegate ()<EMClientDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //初始化环信
    //AppKey:注册的AppKey
    EMOptions *options = [EMOptions optionsWithAppkey:@"evisuim#evisuim"];
    [[EMClient sharedClient] initializeSDKWithOptions:options];
    [[EMClient sharedClient] addDelegate:self delegateQueue:nil];
    
    [self initWindow];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    
    //环信
    [[EMClient sharedClient] applicationDidEnterBackground:application];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    
    //环信
    [[EMClient sharedClient] applicationWillEnterForeground:application];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (void)initWindow {

    //初始化window
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    BOOL isAutoLogin = [EMClient sharedClient].options.isAutoLogin;
    
    if (isAutoLogin) {
        
        //设置了自动登录
        MainTabBarController *mainController = [[MainTabBarController alloc] init];
        
        self.window.rootViewController = mainController;
        
    } else {
        
        //没有自动登录
        DMLoginViewController *loginVC = [[DMLoginViewController alloc] init];
        
        self.window.rootViewController = loginVC;
        
    }
    
    [self.window makeKeyAndVisible];
}

#pragma mark 环信自动登录回调
- (void)didAutoLoginWithError:(EMError *)aError {
    
    //发出自动登录通知
    [[NSNotificationCenter defaultCenter] postNotificationName:KAUTOLOGIN object:nil];
}


#pragma mark 环信重连
/*!
 *  SDK连接服务器的状态变化时会接收到该回调
 *
 *  有以下几种情况，会引起该方法的调用：
 *  1. 登录成功后，手机无法上网时，会调用该回调
 *  2. 登录成功后，网络状态变化时，会调用该回调
 *
 *  @param aConnectionState 当前状态
 */
- (void)didConnectionStateChanged:(EMConnectionState)aConnectionState {

    [BaseNotificationCenter postNotificationName:KCONNECTION object:nil];
}

#pragma mark 环信被动退出登录
/*!
 *  当前登录账号在其它设备登录时会接收到该回调
 */
- (void)didLoginFromOtherDevice {

    [BaseNotificationCenter postNotificationName:KLOGIN_FROM_OTHER_DEVICE object:nil];
}

/*!
 *  当前登录账号已经被从服务器端删除时会收到该回调
 */
- (void)didRemovedFromServer{

    [BaseNotificationCenter postNotificationName:KACCOUNT_ERROR object:nil];
}

@end
